package com.cw.jeeyt.service.impl.system;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemAttachDirBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemAttachDirDao;
import com.cw.jeeyt.dao.model.system.SystemAttachDirDo;
import com.cw.jeeyt.service.converter.system.SystemAttachDirConverter;
import com.cw.jeeyt.service.intf.system.SystemAttachDirService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 附件路径业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class SystemAttachDirServiceImpl implements SystemAttachDirService {

    private static final Logger logger = LoggerFactory.getLogger(SystemAttachDirServiceImpl.class);
    @Autowired
    private SystemAttachDirDao systemAttachDirDao;

    /**
     * 分页查询附件路径
     *
     * @param systemAttachDirBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemAttachDirBo> queryByPage(SystemAttachDirBo systemAttachDirBo, Integer startNum,
        Integer pageSize) throws Exception {
        PageInfo<SystemAttachDirDo> pageInfo = systemAttachDirDao.queryByPage(
            SystemAttachDirConverter.bo2Do(systemAttachDirBo), startNum, pageSize);
        return new PageInfo<>(pageInfo, SystemAttachDirConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻附件路径信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemAttachDirBo getSystemAttachDirById(String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            logger.error("--主键id为空--");
            return null;
        }

        return SystemAttachDirConverter.do2Bo(systemAttachDirDao.getById(id));
    }

    /**
     * 新增附件路径
     *
     * @param systemAttachDirBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insert(SystemAttachDirBo systemAttachDirBo) throws Exception {

        if (systemAttachDirBo == null) {
            logger.error("附件路径为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemAttachDirDo systemAttachDir = SystemAttachDirConverter.bo2Do(systemAttachDirBo);
        systemAttachDir.setId(SequenceTool.nextId());
        systemAttachDir.setCreateBy(userVo.getId());
        systemAttachDir.setCreateDate(new Date());
        systemAttachDir.setUpdateBy(userVo.getId());
        systemAttachDir.setUpdateDate(new Date());
        return systemAttachDirDao.insert(systemAttachDir);
    }

    /**
     * 更新附件路径信息
     *
     * @param systemAttachDirBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SystemAttachDirBo systemAttachDirBo) throws Exception {

        if (systemAttachDirBo == null) {
            logger.error("附件路径为空");
            return;
        }

        SystemAttachDirDo systemAttachDir = SystemAttachDirConverter.bo2Do(systemAttachDirBo);
        systemAttachDir.setUpdateBy(SessionUtil.getSessionUserId());
        systemAttachDir.setUpdateDate(new Date());

        systemAttachDirDao.update(systemAttachDir);
    }

    /**
     * 删除附件路径
     *
     * @param systemAttachDirBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SystemAttachDirBo systemAttachDirBo) throws Exception {

        if (systemAttachDirBo == null) {
            logger.error("附件路径为空");
            return;
        }
        systemAttachDirDao.delete(SystemAttachDirConverter.bo2Do(systemAttachDirBo));
    }
}
